/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.ec.controller;

import cn.iot.card.open.annotion.Permission;
import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.ec.service.EcBlocSynService;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

/**
 * 集团综合信息控制器
 *
 * @author 何光鑫
 * @date 20200108 09:00:03
 * @modify 20200108 何光鑫 v1.1.1 创建
 * @since v1.1.1
 */
@Controller
@RequestMapping("/blocSyn")
public class EcBlocSynController extends BaseController {

    /**
     * 集团综合信息页面路径前缀
     */
    private static String PREFIX = "/modular/ec/blocSyn/";

    @Autowired
    private EcBlocSynService ecBlocSynService;

    /**
     * 跳转到集团综合信息查询列表页面
     *
     * @return 返回 string
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "blocSyn.html";
    }

    /**
     * 查询集团账号信息列表
     *
     * @param accountId 集团id
     * @return 返回
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) Long accountId) throws IOException{
        String transId = CommonUtil.generateTransId(String.valueOf(accountId));
        return ecBlocSynService.selectBlocByAccountId(transId,accountId);
    }

    /**
     * 跳转到查询集团短信白名单页面
     *
     * @param accountId 集团id
     * @param model
     * @return 返回 string
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/blocSyn_whiteList/{accountId}")
    public String blocWhiteList(@PathVariable("accountId") String accountId, Model model) {
        if (ToolUtil.isEmpty(accountId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("accountId", accountId);
        return PREFIX + "blocSyn_whiteList.html";
    }

    /**
     * 通过 msisdn 精确查找集团短信白名单列表.
     *
     * @param accountId 集团id
     * @param msisdn
     * @return 返回 white list
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/blocWhiteLists")
    @ResponseBody
    public LayuiPageInfo getblocWhiteList(@RequestParam String accountId, @RequestParam(required = false) String msisdn) throws IOException{
        if (ToolUtil.isEmpty(accountId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String transId = CommonUtil.generateTransId(accountId);
        return ecBlocSynService.queryWhiteList(transId, accountId, msisdn);
    }

    /**
     * 跳转到集团已订购资费页面
     *
     * @param accountId 集团id
     * @param model
     * @return 返回 string
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/blocSyn_orderCharges/{accountId}")
    public String blocOrderCharges(@PathVariable("accountId") String accountId, Model model) {
        if (ToolUtil.isEmpty(accountId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("accountId", accountId);
        return PREFIX + "blocSyn_orderCharges.html";
    }

    /**
     * 查询集团已订购资费列表.
     *
     * @param accountId 集团id
     * @return 返回 bloc order charges
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/blocOrderCharges")
    @ResponseBody
    public LayuiPageInfo getBlocOrderCharges(@RequestParam String accountId) throws IOException{
        if (ToolUtil.isEmpty(accountId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String transId = CommonUtil.generateTransId(accountId);
        Map<String, String> parameterMap = new HashMap<>();
        parameterMap.put(Constants.REQUEST_QUERY_TYPE, Constants.REQUEST_QUERY_TYPE_CUST);
        return ecBlocSynService.queryOrderCharges(transId, accountId,parameterMap);
    }

    /**
     * 查询物联卡生命周期分布
     *
     * @param accountId 集团id
     * @return 返回 status number
     * @throws IOException
     * @author 何光鑫
     * @date 20200110 17:13:16
     * @modify 20200110 何光鑫 v1.0 创建
     * @since v1.0
     */
    @Async
    @RequestMapping("/statusNumber")
    @ResponseBody
    public Object getStatusNumber(@RequestParam String accountId) throws IOException{
        String transId = CommonUtil.generateTransId(accountId);
        Map<String, Object> maps = ecBlocSynService.queryStatusNumber(transId, accountId);
        return maps;
    }

    /**
     * 按月查询集团总账单
     *
     * @param accountId 集团id
     * @param queryDate 查询日期
     * @return 返回 ec bill
     * @throws IOException
     * @author 何光鑫
     * @date 20200110 17:13:19
     * @modify 20200110 何光鑫 v1.0 创建
     * @since v1.0
     */
    @Async
    @RequestMapping("/ecBill")
    @ResponseBody
    public Object getEcBill(@RequestParam String accountId,@RequestParam String queryDate) throws IOException{
        String transId = CommonUtil.generateTransId(accountId);
        return ecBlocSynService.queryEcBill(transId, accountId, queryDate);
    }

    /**
     * 物联卡风险等级分布按月查询
     *
     * @param accountId
     * @param queryDate
     * @return 返回 risk level
     * @throws IOException 描述抛出异常原因
     * @author 何光鑫
     * @date 20200110 17:17:01
     * @modify 20200110 何光鑫 v1.0 创建
     * @since v1.0
     */
    @Async
    @RequestMapping("/riskLevel")
    @ResponseBody
    public Object getRiskLevel(@RequestParam String accountId,@RequestParam String queryDate) throws IOException{
        String transId = CommonUtil.generateTransId(accountId);
        return ecBlocSynService.queryRiskLevel(transId, accountId,queryDate);
    }

    /**
     * 高风险物联卡类型分布查询
     *
     * @param accountId 集团id
     * @param queryDate 查询日期
     * @return 返回 high risk
     * @throws IOException
     * @author 何光鑫
     * @date 20200110 17:17:04
     * @modify 20200110 何光鑫 v1.0 创建
     * @since v1.0
     */
    @Async
    @RequestMapping("/highRisk")
    @ResponseBody
    public Object getHighRisk(@RequestParam String accountId,@RequestParam String queryDate) throws IOException{
        String transId = CommonUtil.generateTransId(accountId);
        return ecBlocSynService.queryHighRisk(transId, accountId,queryDate);
    }
}
